﻿package  {
	
	import flash.display.Sprite;
	import flash.geom.Vector3D;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	/**
		This is the background board which handles the mouse events. Trying to make this as stupid as possible.
	*/
	
	public class SpriteBoard extends Sprite {

		private static const BG_COLOUR = 0x1A3442;
		private static const DEFAULT_VELX = 2;
		private static const DEFAULT_VELY = -4;
		private static const DEFAULT_VELMAG = 6;
		
		private var bgSprite:Sprite;
		private var object3D:Object3D;
		
		// mouse displacement variables
		private var doRotate:Boolean = false;
		private var velMag:Number;
		private var prevX:Number;
		private var prevY:Number;
		private var velX:Number;
		private var velY:Number;
		
		private var tempVelX:Number;
		private var tempVelY:Number;
		
		public function SpriteBoard(bgWidth:Number, bgHeight:Number) {
			
			bgSprite = new Sprite();
			this.addChild(bgSprite);
			
			object3D = new Object3D(this);
			
			
			
			
			

			// centralise so that the cube draws correctly.
			this.x = Math.round(bgWidth/2);
			this.y = Math.round(bgHeight/2);
			
			drawBackground(bgWidth, bgHeight);
			
			
			// set the cube spinning defined by RNG
			velX = (Math.random() * 8) - 4;
			velY = (Math.random() * 12) - 6;
			velMag = DEFAULT_VELMAG;
			
			initMouseEvents();
			this.addEventListener(Event.ENTER_FRAME,whenEnterFrame);
			
		}
		
		public function drawBackground(bgWidth:Number, bgHeight:Number) {
			this.graphics.beginFill(BG_COLOUR);
			this.graphics.drawRect(-bgWidth/2, -bgHeight/2, bgWidth, bgHeight);
			this.graphics.endFill();
		}
		
		public function initMouseEvents()
		{
			this.addEventListener(MouseEvent.MOUSE_MOVE, bgMove);
			this.addEventListener(MouseEvent.MOUSE_DOWN, bgDown);
			this.addEventListener(MouseEvent.MOUSE_UP, bgUp);
			this.addEventListener(MouseEvent.ROLL_OUT, bgOut);
			this.addEventListener(MouseEvent.MOUSE_OVER, bgOver);
		}
		
		public function haltRotation():void {
			tempVelX = velX;
			tempVelY = velY;
			
			velX /= 5;
			velY /= 5;
		}
		
		public function startRotation():void {
			velX = tempVelX;
			velY = tempVelY;
		}
		
		// Event functions
		public function whenEnterFrame(e:Event):void {
			if(!doRotate && velMag > 0) {
				
					velY *= 0.95;
					velX *= 0.95;
					//velMag *= 0.1;
				
				object3D.rotateAndDraw(velY,velX,0);
			}
		}
		public function bgOver(e:MouseEvent):void {
			object3D.showText();
		}
		
		public function bgOut(e:MouseEvent):void {
			doRotate = false;
			object3D.hideText();
		}

		public function bgDown(e:MouseEvent):void {
			
			prevX = this.mouseX;
			prevY = this.mouseY;
			
			velX=0;
			velY=0;
			velMag=0;
			
			doRotate = true;
		}

		public function bgUp(e:MouseEvent):void {
			doRotate = false;
		}
		

		public function bgMove(e:MouseEvent):void {
			var locX:Number = prevX;
			var locY:Number = prevY;

			if (doRotate) {
				prevX = this.mouseX;
				prevY = this.mouseY;
				
				// calculate the velocity for AR
				velX=-2*(prevX-locX);
				velY= 2*(prevY-locY);
				velMag=Math.abs(velX)+Math.abs(velY);

				object3D.rotateAndDraw(prevY-locY,-(prevX-locX),0);
				e.updateAfterEvent();
			}
		}
		

	}
	
}
